package cn.tedu.medicalmanagementsystem.manager.service.impl;

import cn.tedu.medicalmanagementsystem.manager.mapper.NoticeMapper;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.AddNoticeParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.NoticeQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.UpdateNoticeParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.entity.Notice;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.NoticeVO;
import cn.tedu.medicalmanagementsystem.manager.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    NoticeMapper noticeMapper;
    @Override
    public List<NoticeVO> getNoticeList() {
        log.debug("开始查询公告数据");
        return noticeMapper.selectNotices();
    }

    @Override
    public void addViewCount(Long id) {
        log.debug("开始添加浏览量");
        noticeMapper.addViewCount(id);

        log.debug("添加浏览量成功");
    }

    @Override
    public void addNotice(AddNoticeParam addNoticeParam) {
        log.debug("开始添加公告: {}", addNoticeParam.getTitle());

        Notice notice = new Notice();
        // 修正参数顺序：源对象在前，目标对象在后
        BeanUtils.copyProperties(addNoticeParam, notice);
        notice.setViewCount(0);
        notice.setPublishTime(new Date());

        noticeMapper.insert(notice);

        log.debug("公告添加成功，ID: {}", notice.getId());
    }

    @Override
    public void updateNotice(UpdateNoticeParam updateNoticeParam) {
        log.debug("开始修改公告: {}", updateNoticeParam.getTitle());

        // 检查公告是否存在
        Notice existingNotice = noticeMapper.selectById(updateNoticeParam.getId());
        if (existingNotice == null) {
            throw new RuntimeException("公告不存在，ID: " + updateNoticeParam.getId());
        }

        // 更新公告
        Notice notice = new Notice();
        BeanUtils.copyProperties(updateNoticeParam, notice);

        // 保留原有浏览量和发布时间
        notice.setViewCount(existingNotice.getViewCount());
        notice.setPublishTime(existingNotice.getPublishTime());


        int rows = noticeMapper.updateById(notice);
        if (rows != 1) {
            throw new RuntimeException("更新公告失败，受影响行数: " + rows);
        }

        log.debug("修改公告成功，ID: {}", updateNoticeParam.getId());
    }

    @Override
    public void deleteNoticeById(Long id) {
        log.debug("开始删除公告，ID: {}", id);

        int rows = noticeMapper.deleteById(id);

        if (rows != 1) {
            throw new RuntimeException("删除失败，公告不存在或已被删除");
        }

        log.debug("公告删除成功，ID: {}", id);
    }

    @Override
    public NoticeVO selectNotice(NoticeQuery noticeQuery) {
        log.debug("开始查询公告，ID: {}", noticeQuery);
        NoticeVO noticeVO = noticeMapper.selectNotice(noticeQuery);
        if (noticeVO == null) {
            throw new RuntimeException("公告不存在");
        }
        return noticeVO;
    }
}
